if orders and not orders.GetStopPrice then


function orders:GetStopPrice(order)
	if order.stop_order_type == 10			-- activated t&s
		or order.stop_order_type == 9		-- t&s
	then
		return order.condition_price2
	elseif order.stop_order_type == 7		-- activated stop
		or order.stop_order_type == 1		-- stop
	then
		return order.condition_price
	end
end


function orders:GetTakePrice(order)
	if order.stop_order_type == 10			-- activated t&s
		or order.stop_order_type == 9		-- t&s
	then
		return order.condition_price
	elseif order.stop_order_type == 1		-- stop
	then
		local limit = self:GetTakeLimit(order)
		if limit then
			return limit.price
		end
	end
end


function orders:SetStopPrice(order, price)
	if price and price > 0 then
		local instrument = lib:GetInstrument(order)
		
		if instrument then
			if not self:IsOrder(order) then
				--message("SetStopPrice not IsOrder")
				
				self:SaveStop(instrument.code, price)
				
				local exist
				local stops
				
				if type(order) == "string" or lib:IsInstrument(order) then
					stops = self:GetAllStops(instrument.code)
				else
					stops = order					-- list of orders
				end
				
				for _, o in pairs(stops) do
					--message("SetStopPrice loop " .. tostring(o.order_num))
				
					if self:SetStopPrice(o, price) then
						exist = true
					end
				end
				
				return exist
				
			elseif self:IsStopOrder(order)
				and not self.stop_replaced[order.order_num]
				and bit.band(order.flags, 3) == 1		-- active
				and lib:CheckStop(order.sec_code, bit.test(order.flags, 2), price)
			then
				--message("SetStopPrice order " .. order.order_num)
				
				local transaction = self:KillStopTransaction(order)
				local next_transaction
				local limit
				
				if order.stop_order_type == 1		-- stop
				then
					limit = self:GetTakeLimitID(order.order_num)
				end
				
				if order.stop_order_type == 10			-- activated t&s
					or order.stop_order_type == 9		-- t&s
				then
					if bit.test(order.flags, 2) then	-- short
						if price <= order.condition_price2 * 0.995 then return end
						if price >= order.condition_price then return end
					else								-- long
						if price >= order.condition_price2 * 1.005 then return end
						if price <= order.condition_price then return end
					end
					
					if transaction.ex__.instrument.min_price_step
						and math.abs(price - order.condition_price2) < transaction.ex__.instrument.min_price_step
						or price == order.condition_price2
					then return end
					
					transaction.ex__.condition_price2 = price
					transaction.ex__.condition_price = order.condition_price
					
					next_transaction =	order.stop_order_type == 10 and self:ActivatedTakeStopTransaction(transaction)
										or order.stop_order_type == 9 and self:TakeStopTransaction(transaction)
										
				elseif order.stop_order_type == 7		-- activated stop
					or order.stop_order_type == 1		-- stop
				then
					if bit.test(order.flags, 2) then	-- short
						if price <= order.condition_price * 0.995 then return end
					else								-- long
						if price >= order.condition_price * 1.005 then return end
					end
					
					if transaction.ex__.instrument.min_price_step
						and math.abs(price - order.condition_price) < transaction.ex__.instrument.min_price_step
						or price == order.condition_price
					then return end
					
					transaction.ex__.condition_price = price
					
					next_transaction =	order.stop_order_type == 7 and self:ActivatedStopTransaction(transaction)
										or order.stop_order_type == 1 and self:StopTransaction(transaction)
					
				else
					return
				end
				
				if limit then
					next_transaction.ex__.take_limit = order.order_num
				end
				
				self.stop_replaced[order.order_num] = lib:tickseconds()
		
				self.log("SetStopPrice " .. price, order)
			
				return self:sendTransaction(transaction, next_transaction)
			end
		end
	end
end


function orders:SetTakePrice(order, price)
	--message("SetTakePrice " .. tostring(price) .. " " .. lib:toline(order))
	if price and price > 0 then
		if type(order) == "string" then				-- sec_code
			self:SaveTake(order, price)
			
			local exist
			
			for _, o in ipairs(self:GetStops(order)) do
				exist = true
				self:SetTakePrice(o, price)
			end
			
			if not exist then
				for _, o in ipairs(self:GetTakeStops(order, true)) do		-- farest take first
					exist = true
					if self:SetTakePrice(o, price) then break end
				end
			end
			
			return exist
		elseif self:IsOrder(order)
			and not self.stop_replaced[order.order_num]
			and bit.band(order.flags, 3) == 1		-- active
		then
			local transaction = self:KillStopTransaction(order)
			local next_transaction
			local limit
			
			if order.stop_order_type == 1		-- stop
			then
				limit = self:GetTakeLimit(order)
				if limit then
					transaction = self:KillOrderTransaction(limit)
					limit.ex__ = {tp = price}
					next_transaction = self:TakeLimitTransaction(limit)
					next_transaction.ex__.take_limit = limit.order_num
				end
			end
			
			if not limit then
				if order.stop_order_type == 10			-- activated t&s
					or order.stop_order_type == 9		-- t&s
				then
					if bit.test(order.flags, 2) then	-- short
						if price <= order.condition_price2 then return end
					else								-- long
						if price >= order.condition_price2 then return end
					end
					
					if transaction.ex__.instrument.min_price_step
						and math.abs(price - order.condition_price) < transaction.ex__.instrument.min_price_step
						or price == order.condition_price
					then return end
					
					transaction.ex__.condition_price = price
					transaction.ex__.condition_price2 = order.condition_price2
					
					next_transaction =	order.stop_order_type == 10 and self:ActivatedTakeStopTransaction(transaction)
										or order.stop_order_type == 9 and self:TakeStopTransaction(transaction)
				
				elseif order.stop_order_type == 7		-- activated stop
					or order.stop_order_type == 1		-- stop
				then
					if bit.test(order.flags, 2) then	-- short
						if price <= order.condition_price then return end
					else								-- long
						if price >= order.condition_price then return end
					end
					
					transaction.ex__.condition_price = price
					transaction.ex__.condition_price2 = order.condition_price
					
					next_transaction =	order.stop_order_type == 7 and self:ActivatedTakeStopTransaction(transaction)
										or order.stop_order_type == 1 and self:TakeStopTransaction(transaction)
				else
					return
				end
			end
	
			self.log("SetTakePrice " .. price, order)
			
			self.stop_replaced[order.order_num] = lib:tickseconds()
		
			return self:sendTransaction(transaction, next_transaction)
		end
	end
end


end -- of file
